﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.Searchs
{
    public partial class Student_accounting_SearchUC : SearchUC
    {
        public Student_accounting_SearchUC()
        {
            InitializeComponent();
            LoadDataBranch();
            DateEdit_Registerfrom.DateTime = DateTime.Now.AddMonths(-6);
            DateEdit_RegisterTo.DateTime = DateTime.Now;
        }
        string CourseSearchString = "1=1";
        string ClassSearchString = "1=1";

    
 


        void LoadDataBranch()
        {
            var data = (List<Branch>)RegisterService.RegisterServices.BranchService.GetList();
            data.Insert(0, new Branch()
            {
                Active = true,
                Code = "Tất cả",
                Name = "Tất cả",
                ID = 0
            });
            lookUpEdit_Branch.Properties.DataSource = data.OrderBy(c => c.ID);
            lookUpEdit_Branch.Properties.ValueMember = "ID";
            lookUpEdit_Branch.Properties.DisplayMember = "Name";
            string[] columns = { "Code", "Name" };
            for (int i = 0; i < lookUpEdit_Branch.Properties.Columns.Count; i++)
            {
                {
                    var index = Array.IndexOf(columns, lookUpEdit_Branch.Properties.Columns[i].FieldName);
                    if (index < 0)
                    {
                        lookUpEdit_Branch.Properties.Columns[i].Visible = false;
                        lookUpEdit_Branch.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                    }
                }
            }
        }


        void LoadDataCourse()
        {
            var data = (List<Course>)RegisterService.RegisterServices.CourseService.GetList(CourseSearchString);
            data.Insert(0, new Course()
            {
                Active = true,
                Code = "Tất cả",
                Name = "Tất cả",
                ID = 0
            });
            lookUpEdit_Course.Properties.DataSource = data.OrderBy(c => c.ID);
            lookUpEdit_Course.Properties.ValueMember = "ID";
            lookUpEdit_Course.Properties.DisplayMember = "Name";
            string[] columns = { "Code", "Name" };
            for (int i = 0; i < lookUpEdit_Course.Properties.Columns.Count; i++)
            {
                {
                    var index = Array.IndexOf(columns, lookUpEdit_Course.Properties.Columns[i].FieldName);
                    if (index < 0)
                    {
                        lookUpEdit_Course.Properties.Columns[i].Visible = false;
                        lookUpEdit_Course.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                    }
                }
            }
        }

        void LoadDataClass()
        {
            var data = (List<Class>)RegisterService.RegisterServices.ClassService.GetList(ClassSearchString);
            data.Insert(0, new Class()
            {
                Active = true,
                Code = "Tất cả",
                Name = "Tất cả",
                ID = 0
            });
            lookUpEdit_Class.Properties.DataSource = data.OrderBy(c => c.ID);
            lookUpEdit_Class.Properties.ValueMember = "ID";
            lookUpEdit_Class.Properties.DisplayMember = "Name";
            string[] columns = { "Code", "Name" };
            for (int i = 0; i < lookUpEdit_Class.Properties.Columns.Count; i++)
            {
                {
                    var index = Array.IndexOf(columns, lookUpEdit_Class.Properties.Columns[i].FieldName);
                    if (index < 0)
                    {
                        lookUpEdit_Class.Properties.Columns[i].Visible = false;
                        lookUpEdit_Class.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                    }
                }
            }
        }

        private void cmdtim_Click(object sender, EventArgs e)
        {
            string search ="";
            var code = txtcode.Text;
            var name = txtname.Text;
            var datefrom = DateEdit_Registerfrom.DateTime;
            var dateto = DateEdit_RegisterTo.DateTime;
            long status =0;
            long branch = 0;
            long classid = 0;
            long course = 0;
            int coursetype = 0;
            if (comboBoxEdit_Status.SelectedIndex != 0)
            {
                status = (long)(comboBoxEdit_Status.SelectedIndex);
            }
         

            if (lookUpEdit_Class.EditValue != null)
            {
                classid = (long)(lookUpEdit_Class.EditValue);
            }

            if (lookUpEdit_Branch.EditValue != null)
            {
                branch = (long)(lookUpEdit_Branch.EditValue);
            }

            if (lookUpEdit_Course.EditValue != null)
            {
                course = (long)(lookUpEdit_Course.EditValue);
            }
            if(radioGroup_CourseType.SelectedIndex!=0)
            {
                coursetype = radioGroup_CourseType.SelectedIndex;
            }

            if (!String.IsNullOrEmpty(code) && code != "Tất cả")
            {
                search += " Student.Code like N'%" + code + "5' and";
            }
            if (!String.IsNullOrEmpty(name) && name != "Tất cả")
            {
                search += " Student.Name like N'%" + name + "%' and";
            }
            if (datefrom > dateto)
            {
                MessageControl.ShowMessage("Ngày từ phải nhỏ hơn hoặc bằng ngày đến", "Error", MessageControl.Icon.Error);
                return;
            }
            //convert cho ra ket qua la yyyy-MM-DD
            search += " Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + datefrom.ToString("yyyy-MM-dd") + "' and '" + dateto.ToString("yyyy-MM-dd") + "' and";
            if (status > 0)
            {
                search += " Student_Fee.Status =" + (status-1) + " and";
            }
          

            if (classid > 0)
            {
                search += " Student.ClassID =" + classid + " and";
            }
            if (course > 0)
            {
                search += " Course.ID =" + course + " and";
            }
            if (branch > 0)
            {
                search += " Branch.ID =" + branch + " and";
            }
            if (coursetype > 0)
            {
                if (coursetype == 1)
                {
                    coursetype = 1;
                }
                else
                {
                    coursetype = 0;
                }
                search += " Class.Type =" + coursetype + " and";
            }
            search = search.Remove(search.Length - 3, 3);
            Master.SearchString = search;
         
        }

      

      
        private void lookUpEdit_Course_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            LoadDataCourse(); lookUpEdit_Course.ShowPopup();
        }

        private void lookUpEdit_Course_Click(object sender, EventArgs e)
        {
            LoadDataCourse(); lookUpEdit_Course.ShowPopup();
        }

        private void lookUpEdit_Class_Click(object sender, EventArgs e)
        {
            LoadDataClass(); lookUpEdit_Class.ShowPopup();
        }

        private void lookUpEdit_Class_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            LoadDataClass(); lookUpEdit_Class.ShowPopup();
        }

        private void radioGroup_CourseType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup_CourseType.SelectedIndex == 0)
            {
                CourseSearchString = "1=1";
                ClassSearchString = "1=1";
            }
            if (radioGroup_CourseType.SelectedIndex == 1)
            {
               // CourseSearchString = "Class.Type =1";
                ClassSearchString = "Class.Type =1";
            }
            if (radioGroup_CourseType.SelectedIndex == 2)
            {
               // CourseSearchString = "Class.Type =0";
                ClassSearchString = "Class.Type =0";
            }
        }

        private void simpleButton_close_Click(object sender, EventArgs e)
        {
            ClosePanel();
        }

        

    }
}
